<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/**
 * CSVReader Class
 *
 * $Id: csvreader.php 147 2007-07-09 23:12:45Z Pierre-Jean $
 *
 * Allows to retrieve a CSV file content as a two dimensional array.
 * The first text line shall contains the column names.
 *
 * @author        Pierre-Jean Turpeau
 * @link        http://www.codeigniter.com/wiki/CSVReader
 */
class CSVReader {

	var $fields;
	/** columns names retrieved after parsing */
	var $separator = ';';
	/** separator used to explode each line */

	/**
	 * Parse a text containing CSV formatted data.
	 *
	 * @access    public
	 * @param    string
	 * @return    array
	 */
	function parse_text($p_Text) {

		$lines = file($p_Text);

		return $this -> parse_lines($lines);
	}

	/**
	 * Parse to db containing CSV formatted data.
	 *
	 * @access    public
	 * @param    string
	 * @return    array
	 */
	function parse_to_db($data, $filterField = array(), $callback_values = array()) {
		$lines = file($data);
		$call_s = array_merge(array('normalize_fields_to_db'), $filterField);
		return $this -> parse_lines_to_db($lines, $call_s, $callback_values);
	}

	/**
	 * Parse a file containing CSV formatted data.
	 *
	 * @access    public
	 * @param    string
	 * @return    array
	 */
	function parse_file($p_Filepath) {
		$lines = file($p_Filepath);
		return $this -> parse_lines($lines);
	}

	/**
	 * Parse an array of text lines containing CSV formatted data.
	 *
	 * @access    public
	 * @param    array
	 * @return    array
	 */
	function parse_lines($p_CSVLines) {
		$content = FALSE;
		foreach ($p_CSVLines as $line_num => $line) {
			if ($line != '') {// skip empty lines
				$elements = explode($this -> separator, $line);

				if (!is_array($content)) {// the first line contains fields names
					$this -> fields = $elements;
					$content = array();
				} else {
					$item = array();
					foreach ($this->fields as $id => $field) {
						if (isset($elements[$id])) {
							$item[trim($field)] = $elements[$id];
						}
					}
					$content[] = $item;
				}
			}
		}
		return $content;
	}

	/**
	 * Parse an array of text lines containing CSV formatted data.
	 *
	 * @access    public
	 * @param    array
	 * @return    array
	 */
	function parse_lines_to_db($p_CSVLines, $call, $callback_values) {
		$content = FALSE;
		foreach ($p_CSVLines as $line_num => $line) {
			if ($line != '') {// skip empty lines
				$elements = explode($this -> separator, $line);

				if (!is_array($content)) {// the first line contains fields names
					$columns = $this -> normalize_field($elements, $call);
					$content = array();
				} else {
					$item = array();
					foreach ($elements as $id => $value) {
						if (isset($columns[$id])) {
							 $column = $columns[$id];	
							 $item[$column] = $value;
							if(isset($callback_values[$column])){
						   	$item[$column]= call_user_func($callback_values[$column], $value);
						   }	
						}
					}
					$content[] = $item;
				}
			}
		}
		return $content;
	}

	private function normalize_field($fields, $call) {
		if (!is_array($call)) {
			$call = array($call);
		}

		$rfields = array();
		foreach ($fields as $field) {

			foreach ($call as $fn) {
				$field = call_user_func($fn, $field);
			}
			$rfields[] = $field;
		}
		return $rfields;

	}

}
